#! /usr/bin/make -f
#
#	debian/rules file for lusca.
#

# Version.
package = lusca

# Include dpatch stuff.
include /usr/share/dpatch/dpatch.make

SHELL	= /bin/bash
LC_ALL	= POSIX

#roelf: debug build
DEB_BUILD_OPTIONS=nostrip noopt

DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU 2>/dev/null)
DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS 2>/dev/null)

CFLAGS = -Wall -g
INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755
     
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#CFLAGS += -O0
CFLAGS += -march=pentium4 -O2 -pipe -fomit-frame-pointer
else
CFLAGS += -O2
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

# Take account of old dpkg-architecture output.
ifeq ($(DEB_HOST_ARCH_CPU),)
  DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
  ifeq ($(DEB_HOST_ARCH_CPU),x86_64)
    DEB_HOST_ARCH_CPU := amd64
  endif
endif
ifeq ($(DEB_HOST_ARCH_OS),)
  DEB_HOST_ARCH_OS := $(subst -gnu,,$(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM))
  ifeq ($(DEB_HOST_ARCH_OS),gnu)
    DEB_HOST_ARCH_OS := hurd
  endif
endif

tmp	     = debian/tmp
bindir       = /usr/sbin
libexecdir   = /usr/lib/squid
sysconfdir   = /etc/squid
logdir       = /var/log/squid
docdir       = /usr/share/doc/lusca
datadir      = /usr/share/lusca
mibdir       = /usr/share/snmp/mibs

DEFAULTS     =	logdir=$(logdir) \
		DEFAULT_CACHE_LOG=$(logdir)/cache.log \
		DEFAULT_ACCESS_LOG=$(logdir)/access.log \
		DEFAULT_STORE_LOG=$(logdir)/store.log \
		DEFAULT_PID_FILE=/var/run/squid.pid \
		DEFAULT_SWAP_DIR=/var/spool/squid \
		DEFAULT_ICON_DIR=$(datadir)/icons \
		DEFAULT_ERROR_DIR=$(datadir)/errors/English \
		DEFAULT_MIME_TABLE=$(datadir)/mime.conf \
		DEFAULT_MIB_PATH=$(mibdir)/SQUID.txt

# The HURD doesn't have pthreads yet.
ifeq ($(DEB_HOST_ARCH_OS), gnu)
	with_pthreads = --enable-storeio=aufs,coss,null
	with_netfilter =
	with_arp_acl =
	with_epoll =
else
ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
	with_pthreads = --enable-async-io --with-pthreads \
		--enable-storeio=aufs,coss,null
	with_netfilter =
	with_arp_acl =
	with_epoll = --enable-kqueue
else
	with_pthreads = --enable-async-io --with-pthreads \
		--enable-storeio=aufs,coss,null --with-aufs-threads=128
#	with_netfilter = --enable-linux-netfilter --enable-linux-tproxy4
	with_netfilter = --enable-linux-netfilter
	with_arp_acl = 
	with_epoll = --enable-epoll
endif
endif

# setresuid is broken on sparc
ifeq ($(DEB_HOST_ARCH_CPU), sparc)
	opt_ac_cv_func_setresuid = ac_cv_func_setresuid=no
else
	opt_ac_cv_func_setresuid = ac_cv_func_setresuid=yes
endif

define checkdir
	test -f src/squid.h
endef


# Build the binaries.
build: patch config.status
	$(checkdir)
	$(MAKE) $(DEFAULTS) all
	# Authentication modules
	cd helpers/basic_auth/NCSA && make
	cd helpers/basic_auth/SMB && make SAMBAPREFIX=/usr INSTALLBIN=/usr/lib/squid
	cd helpers/basic_auth/PAM && make
	cd helpers/basic_auth/LDAP && make
	cd helpers/basic_auth/YP && make
	cd helpers/basic_auth/MSNT && make
	cd helpers/basic_auth/getpwnam && make
	cd helpers/digest_auth/password && make
	cd helpers/digest_auth/ldap && make
	cd helpers/external_acl/ip_user && make
	cd helpers/external_acl/ldap_group && make
	cd helpers/external_acl/session && make
	cd helpers/external_acl/unix_group && make
	cd helpers/external_acl/wbinfo_group && make
	cd helpers/ntlm_auth/SMB && make
	touch build

# Configure the package.
config.status: 

	# use automake to create ./configure
	./bootstrap.sh
	$(opt_ac_cv_func_setresuid) \
	./configure \
	'LIBS=-ltcmalloc' \
	--enable-stacktraces \
	--prefix=/usr \
	--exec_prefix=/usr \
	--bindir=/usr/sbin --sbindir=/usr/sbin \
	--libexecdir=/usr/lib/squid \
	--sysconfdir=$(sysconfdir) \
	--localstatedir=/var/spool/squid \
	--datadir=$(datadir) \
	$(with_pthreads) \
	$(with_netfilter) \
	$(with_arp_acl) \
	$(with_epoll) \
	--enable-removal-policies=lru,heap \
	--enable-snmp \
	--enable-delay-pools \
	--enable-cache-digests \
	--enable-underscores \
	--enable-referer-log \
	--enable-useragent-log \
	--enable-auth="basic,digest,ntlm,negotiate" \
	--enable-carp \
	--enable-follow-x-forwarded-for \
	--with-large-files \
	--with-maxfd=65535 \
	--with-aio \
	--with-dl \
	--enable-http-gzip \
	--enable-http-violations \
	$(DEB_HOST_ARCH_CPU)-debian-$(DEB_HOST_ARCH_OS)

# Architecture independant files.
binary-indep:	build checkroot
	rm -rf $(tmp) debian/files
	$(checkdir)
	#
	#	Build lusca-common package.
	#
	$(INSTALL_DIR) $(tmp)/DEBIAN
	$(INSTALL_DIR) $(tmp)/usr/share/doc/$(package)-common
	#TODO: need one for lusca
	#$(INSTALL_FILE) ChangeLog.Cacheboy $(tmp)/usr/share/doc/$(package)-common/changelog
	$(INSTALL_FILE) ChangeLog.Cacheboy $(tmp)/usr/share/doc/$(package)-common/changelog
	$(INSTALL_FILE) debian/changelog \
		$(tmp)/usr/share/doc/$(package)-common/changelog.Debian
	$(INSTALL_FILE) debian/copyright $(tmp)/usr/share/doc/$(package)-common
	$(INSTALL_DIR) $(tmp)/usr/share/$(package)
	$(INSTALL_DIR) $(tmp)/usr/share/$(package)/errors
	$(INSTALL_DIR) $(tmp)/usr/share/$(package)/icons
	$(INSTALL_DIR) $(tmp)/usr/share/snmp/mibs
	$(INSTALL_FILE) src/mib.txt ./$(tmp)$(mibdir)/SQUID.txt
	$(INSTALL_FILE) src/mime.conf.default ./$(tmp)$(datadir)/mime.conf
	#
	cp -R errors/* ./$(tmp)$(datadir)/errors
	rm -f ./$(tmp)$(datadir)/errors/Makefile*
	rm -f ./$(tmp)$(datadir)/errors/list
	chmod 755 ./$(tmp)$(datadir)/errors/*
	chmod 644 ./$(tmp)$(datadir)/errors/*/*
	chown -R root:root ./$(tmp)$(datadir)/errors
	$(INSTALL_FILE) icons/*.gif ./$(tmp)$(datadir)/icons
	$(INSTALL_DIR) $(tmp)/usr/share/doc/$(package)
	$(INSTALL_DIR) $(tmp)/usr/share/doc/$(package)/examples
	$(INSTALL_FILE) helpers/basic_auth/SMB/README \
		$(tmp)$(docdir)/README.auth_module.smb_auth
	umask 022; head -19 helpers/basic_auth/NCSA/ncsa_auth.c > \
		$(tmp)$(docdir)/README.auth_module.ncsa_auth
	umask 022; head -56 helpers/basic_auth/PAM/pam_auth.c > \
		$(tmp)$(docdir)/README.auth_module.pam_auth
	$(INSTALL_FILE) helpers/basic_auth/MSNT/README.html \
		$(tmp)$(docdir)/README.auth_module.msnt_auth.html
	umask 022; head -21 helpers/basic_auth/getpwnam/getpwnam_auth.c > \
		$(tmp)$(docdir)/README.auth_module.getpwnam_auth
	umask 022; head -26 helpers/digest_auth/password/digest_pw_auth.c > \
		$(tmp)$(docdir)/README.auth_module.digest_pw_auth
	$(INSTALL_FILE) helpers/external_acl/ip_user/README \
		$(tmp)$(docdir)/README.auth_module.ip_user
	$(INSTALL_FILE) helpers/external_acl/ip_user/example.conf \
		$(tmp)$(docdir)/examples/ip_user-example.conf
	$(INSTALL_FILE) helpers/external_acl/unix_group/README \
		$(tmp)$(docdir)/README.auth_module.unix_group
	$(INSTALL_FILE) helpers/negotiate_auth/squid_kerb_auth/readme.txt \
		$(tmp)$(docdir)/README.squid_kerb_auth
	dpkg-gencontrol -isp -p$(package)-common
	dpkg --build $(tmp) ..
	rm -f debian/substvars
	rm -rf $(tmp)

# Make a binary package (.deb file)
binary-arch:	build checkroot
	#
	#	Build lusca package.
	#
	$(INSTALL_DIR) $(tmp)/DEBIAN
	$(INSTALL_DIR) $(tmp)/etc/squid
	$(INSTALL_DIR) $(tmp)/etc/logrotate.d
	$(INSTALL_DIR) $(tmp)/etc/init.d
	$(INSTALL_DIR) $(tmp)/etc/resolvconf/update-libc.d
	$(INSTALL_DIR) $(tmp)/usr/sbin
	$(INSTALL_DIR) $(tmp)/usr/share/doc/$(package)
	$(INSTALL_DIR) $(tmp)/usr/share/doc/$(package)/examples
	$(INSTALL_DIR) $(tmp)/usr/lib/squid
	$(INSTALL_DIR) $(tmp)/var/{log,spool}
	install -m 750 -o proxy -g proxy -d $(tmp)/var/log/squid
	install -m 750 -o proxy -g proxy -d $(tmp)/var/spool/squid
	$(INSTALL_DIR) $(tmp)/usr/share/man/man8
	#
	$(INSTALL_PROGRAM) src/squid $(tmp)/usr/sbin

	$(INSTALL_PROGRAM) src/unlinkd src/logfile-daemon $(tmp)/usr/lib/squid
#	$(INSTALL_PROGRAM) src/logfile-daemon $(tmp)/usr/lib/squid
	$(INSTALL_PROGRAM) app/ufs_rebuild/ufs_rebuild app/ufs_newfs/ufs_newfs $(tmp)/usr/lib/squid
	$(INSTALL_PROGRAM) app/coss_rebuild/coss_rebuild app/coss_newfs/coss_newfs $(tmp)/usr/lib/squid
	$(INSTALL_FILE) debian/logrotate $(tmp)/etc/logrotate.d/squid
	$(INSTALL_SCRIPT) debian/lusca.resolvconf $(tmp)/etc/resolvconf/update-libc.d/lusca
	$(INSTALL_PROGRAM) helpers/basic_auth/SMB/smb_auth $(tmp)$(libexecdir)
	$(INSTALL_SCRIPT) helpers/basic_auth/SMB/smb_auth.sh $(tmp)$(libexecdir)
	$(INSTALL_PROGRAM) helpers/basic_auth/NCSA/ncsa_auth $(tmp)$(libexecdir)
	$(INSTALL_PROGRAM) -m 2754 -o proxy -g shadow \
		helpers/basic_auth/PAM/pam_auth $(tmp)$(libexecdir)
	$(INSTALL_PROGRAM) helpers/basic_auth/LDAP/squid_ldap_auth \
		$(tmp)$(libexecdir)/ldap_auth
	$(INSTALL_PROGRAM) helpers/basic_auth/YP/yp_auth \
		$(tmp)$(libexecdir)/yp_auth
	$(INSTALL_PROGRAM) helpers/basic_auth/MSNT/msnt_auth \
		$(tmp)$(libexecdir)/msnt_auth
	$(INSTALL_PROGRAM) -m 2754 -o proxy -g shadow \
		helpers/basic_auth/getpwnam/getpwname_auth \
		$(tmp)$(libexecdir)/getpwnam_auth
	$(INSTALL_PROGRAM) helpers/digest_auth/password/digest_pw_auth \
		$(tmp)$(libexecdir)/digest_pw_auth
	$(INSTALL_PROGRAM) helpers/external_acl/ip_user/ip_user_check \
		$(tmp)$(libexecdir)/ip_user_check
	$(INSTALL_PROGRAM) helpers/external_acl/ldap_group/squid_ldap_group \
		$(tmp)$(libexecdir)/squid_ldap_group
	$(INSTALL_FILE) helpers/external_acl/ldap_group/squid_ldap_group.8 \
		$(tmp)/usr/share/man/man8/squid_ldap_group.8
	$(INSTALL_PROGRAM) helpers/external_acl/session/squid_session \
		$(tmp)$(libexecdir)/squid_session
	$(INSTALL_FILE) helpers/external_acl/session/squid_session.8 \
		$(tmp)/usr/share/man/man8/squid_session.8
	$(INSTALL_PROGRAM) helpers/external_acl/unix_group/squid_unix_group \
		$(tmp)$(libexecdir)/squid_unix_group
	$(INSTALL_FILE) helpers/external_acl/unix_group/squid_unix_group.8 \
		$(tmp)/usr/share/man/man8/squid_unix_group.8
	$(INSTALL_SCRIPT) helpers/external_acl/wbinfo_group/wbinfo_group.pl \
		$(tmp)$(libexecdir)/wbinfo_group.pl
	$(INSTALL_PROGRAM) helpers/ntlm_auth/SMB/ntlm_auth \
		$(tmp)$(libexecdir)/ntlm_auth
	# the examples. 
	$(INSTALL_FILE) src/squid.conf.default \
	        $(tmp)/usr/share/doc/$(package)/examples/squid.conf
	$(INSTALL_FILE) debian/default.lusca\
		$(tmp)/usr/share/doc/$(package)/examples
	
	# TODO: needs one for lusca
	#$(INSTALL_FILE) ChangeLog $(tmp)/usr/share/doc/$(package)/changelog
	$(INSTALL_FILE) ChangeLog.Cacheboy $(tmp)/usr/share/doc/$(package)/changelog
	$(INSTALL_FILE) ChangeLog.Squid $(tmp)/usr/share/doc/$(package)/changelog.squid
	$(INSTALL_FILE) debian/changelog \
		$(tmp)/usr/share/doc/$(package)/changelog.Debian
	$(INSTALL_FILE) debian/README.Debian \
		$(tmp)/usr/share/doc/$(package)/README.Debian
	$(INSTALL_FILE) debian/NEWS.Debian \
		$(tmp)/usr/share/doc/$(package)/NEWS.Debian
	gzip -9f `find debian/tmp/usr/share/doc/$(package)/* | grep -v examples`
	$(INSTALL_FILE) debian/copyright $(tmp)/usr/share/doc/$(package)
	$(INSTALL_FILE) doc/squid.8 $(tmp)/usr/share/man/man8
	gzip -9f $(tmp)/usr/share/man/man?/*
	$(INSTALL_SCRIPT) debian/lusca.rc $(tmp)/etc/init.d/squid
	#
	$(INSTALL_SCRIPT) debian/preinst $(tmp)/DEBIAN/preinst
	$(INSTALL_SCRIPT) debian/postinst $(tmp)/DEBIAN/postinst
	$(INSTALL_SCRIPT) debian/prerm $(tmp)/DEBIAN/prerm
	$(INSTALL_SCRIPT) debian/postrm $(tmp)/DEBIAN/postrm
	$(INSTALL_FILE) debian/conffiles $(tmp)/DEBIAN/conffiles
	$(INSTALL_SCRIPT) debian/config $(tmp)/DEBIAN/config

	# overrides file
	$(INSTALL_DIR) $(tmp)/usr/share/lintian/overrides/
	$(INSTALL_FILE) debian/lusca.overrides \
		$(tmp)/usr/share/lintian/overrides/lusca
	
	dpkg-shlibdeps $(tmp)/usr/sbin/* $(tmp)$(libexecdir)/*
	dpkg-gencontrol -isp -p$(package)
	dpkg --build $(tmp) ..
	rm -f debian/substvars
	rm -rf $(tmp)
	#
	#	Generate lusca-cgi package.
	#
	$(INSTALL_DIR) $(tmp)/DEBIAN
	$(INSTALL_DIR) $(tmp)/etc/squid
	$(INSTALL_DIR) $(tmp)/usr/lib/cgi-bin
	$(INSTALL_DIR) $(tmp)/usr/share/doc/lusca-cgi
	$(INSTALL_DIR) $(tmp)/usr/share/man/man8
	#
	$(INSTALL_FILE) tools/cachemgr.conf $(tmp)/etc/squid/cachemgr.conf
	$(INSTALL_PROGRAM) tools/cachemgr.cgi $(tmp)/usr/lib/cgi-bin/
	$(INSTALL_FILE) debian/lusca-cgi.8 $(tmp)/usr/share/man/man8
	#TODO: need one for lusca
	#$(INSTALL_FILE) ChangeLog $(tmp)/usr/share/doc/lusca-cgi/changelog
	$(INSTALL_FILE) ChangeLog.Cacheboy $(tmp)/usr/share/doc/lusca-cgi/changelog
	$(INSTALL_FILE) debian/changelog \
		$(tmp)/usr/share/doc/lusca-cgi/changelog.Debian
	gzip -9f $(tmp)/usr/share/doc/lusca-cgi/*
	$(INSTALL_FILE) debian/copyright $(tmp)/usr/share/doc/lusca-cgi
	gzip -9f $(tmp)/usr/share/man/man8/*
	$(INSTALL_DIR) $(tmp)/usr/share/doc/lusca-cgi/examples
	$(INSTALL_SCRIPT) debian/postrm.lusca-cgi $(tmp)/DEBIAN/postrm
	$(INSTALL_SCRIPT) debian/prerm.lusca-cgi $(tmp)/DEBIAN/prerm
	$(INSTALL_FILE) debian/conffiles.lusca-cgi $(tmp)/DEBIAN/conffiles
	dpkg-shlibdeps tools/cachemgr.cgi
	dpkg-gencontrol -isp -plusca-cgi
	dpkg --build $(tmp) ..
	rm -rf $(tmp)

# Clean up.
clean: clean1 unpatch
clean1:	checkroot
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f helpers/basic_auth/NCSA/{Makefile,ncsa_auth.o,ncsa_auth}
	rm -f helpers/basic_auth/SMB/{smb_auth.o,smb_auth}
	rm -f helpers/basic_auth/PAM/{pam_auth.o,pam_auth}
	rm -f helpers/basic_auth/YP/{Makefile,yp_auth,yp_auth.o,nis_support.o}
	rm -f helpers/basic_auth/MSNT/{Makefile,msnt_auth,*.o}
	rm -f helpers/basic_auth/LDAP/{Makefile,squid_ldap_auth,*.o}
	rm -f helpers/basic_auth/getpwnam/{Makefile,getpwnam_auth.o,getpwnam_auth}
	rm -f helpers/digest_auth/password/{Makefile,digest_pw_auth,digest_pw_auth.o}
	rm -f helpers/external_acl/ip_user/{Makefile,ip_user,dict.o,main.o,match.o}
	rm -f helpers/external_acl/ldap_group/{Makefile,squid_ldap_group,squid_ldap_group.o}
	rm -f helpers/external_acl/unix_group/{Makefile,squid_unix_group,check_group.o}
	rm -f helpers/ntlm_auth/SMB/{Makefile,ntlm_auth,*.o,smbval/*.o}
	rm -f build debian/{substvars,files}
	rm -rf $(tmp) *~
	find . -name '*.bak' -o -name '*~' | xargs -r rm -f --
	#cd icons && /bin/sh ./icons.shar

binary:	binary-indep binary-arch

checkroot:
	$(checkdir)
	test root = "`whoami`"

dist:	binary
	dpkg-source -b

.PHONY: binary binary-arch binary-indep clean checkroot clean1 patch unpatch
